from Common.Utils import blog_driver
from Common.FormHelper import FormHelper
from LoginTest import Login
from selenium.webdriver.common.by import By



class MyQuestion:
    url=""
    driver=""

    def __init__(self):
        self.url="http://49.235.61.184:8080/repo/index"
        self.driver=blog_driver.driver
        self.form = FormHelper(self.driver)
        # 公共自动登录方法，未登录时执行Login().LoginSuTest
        # self.form.auto_login(self.url, login_func=lambda: Login().LoginSuTest())
        self.driver.get(self.url)

        # 检测页面元素

    def pageElements(self):
        elememts_check = {
            "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > nav > ol > li:nth-child(3) > span.ant-breadcrumb-link > span",
            "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-pro-table-list-toolbar-left > div",
            "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > thead > tr > th:nth-child(4)"
        }
        blog_driver.GetScreeShot()
        if self.form.are_all_elements_present(elememts_check, 5):
            print("元素都存在")
        else:
            print("元素存在有问题")

    #新建测试
    def AddnewTest(self):
        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div > button")
        blog_driver.GetScreeShot()

        # 切换到点击新建之后的页面之中，因为新建之后的页面里面的元素的id和没点击新建的时候的元素是一样的，所以要限制一下查找元素的范围
        drawer = self.driver.find_element(By.CLASS_NAME, "ant-drawer-wrapper-body")

        #题库名称
        drawer.find_element(By.CSS_SELECTOR,"#name").send_keys("测试")
        #选择题库类型
        drawer.find_element(By.CSS_SELECTOR,"#mode").click()
        #选择问卷
        select_option =self.driver.find_element(By.CLASS_NAME, "rc-virtual-list-holder-inner")
        option = select_option.find_element(By.XPATH, f'.//div[@title="问卷"]')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", option)
        self.driver.execute_script("arguments[0].click();", option)
        blog_driver.GetScreeShot()
        #确定
        drawer.find_element(By.XPATH,"/html/body/div[4]/div/div[3]/div/div/div[3]/div/div/div[2]/button").click()
        #检测查询功能
    def SearchTest(self):
        self.form.fill_fields({"#name":"测试"})
        blog_driver.GetScreeShot()
        #点击查询
        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(2) > button")
        blog_driver.GetScreeShot()
        #验证结果
        self.form.is_element_present("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr.ant-table-row.ant-table-row-level-0 > td:nth-child(4) > span")


# project=MyQuestion()
# project.pageElements()
# project.AddnewTest()
# project.SearchTest()



